package atom.core2.j2ee.util;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import atom.core0.util.StringUtil;
/**
 * Title: Cookie的支持类
 * Description: 如果设置了domain，则匹配与domain完全相同的
 * Copyright: Copyright (c) 2014
 * @author hike_man@126.com
 */
public class MyCookieManager {
	static private String domain;// 可以配置子域名 .jszx.com

	static public void initDoman(String sdomain) {
		domain = sdomain;
	}

	//cookies[i].getDomain() 竟然总是空,所以无法根据domain获取指定cookies
//	static public String getCookieValue(HttpServletRequest request, String name) {
//		String desvalue = (String) request.getAttribute("cookies_" + name);
//		if (StringUtil.isEmpty(desvalue)) {
//
//			Cookie[] cookies = request.getCookies();
//			if (cookies == null)
//				return "";
//			for (int i = 0; i < cookies.length; i++) {
//				if (cookies[i].getName().equals(name)) {
//					if (StringUtil.isEmpty(domain)) {
//						desvalue = cookies[i].getValue();
//						break;
//					}
//					if (domain.equals(cookies[i].getDomain())) {
//						desvalue = cookies[i].getValue();
//						break;
//					}
//					break;
//				}
//			}
//		}
//		if (StringUtil.isEmpty(desvalue))
//			return "";
//
//		return desvalue;
//	}
	
	//cookies[i].getDomain() 竟然总是空,所以无法根据domain获取指定cookies 只能取第一个
	//因此要求，不要出现同名cookies，否则会出错
	
	//默认情况下生成的cookies，新的子域也无法访问 例如JSESSIONID
	//无法指定JSESSIONID的域
	static public String getCookieValue(HttpServletRequest request, String name) {
		String desvalue = (String) request.getAttribute("cookies_" + name);
		if (StringUtil.isEmpty(desvalue)) {

			Cookie[] cookies = request.getCookies();
			if (cookies == null)
				return "";
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(name)) 
				{
					desvalue = cookies[i].getValue();
					break;
				}
			}
		}
		if (StringUtil.isEmpty(desvalue))
			return "";

		return desvalue;
	}
	// 一种算法是跟用户名混杂 然后加密
	static public String genSessionCookieValue(String usercode) {
		String temp = usercode+ (new Date()).getTime();
		return temp;
	}

	static public void setCookieValue(HttpServletRequest request, HttpServletResponse response, String name,
			String value) {
		Cookie cookie1 = new Cookie(name, value);
		cookie1.setPath("/");
		cookie1.setMaxAge(-1);
		if (!StringUtil.isEmpty(domain))
			cookie1.setDomain(domain);

		response.addCookie(cookie1);
		request.setAttribute("cookies_" + name, value);
	}

	static public void removeCookie(HttpServletRequest request, HttpServletResponse response, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(name)) {
				Cookie cookie = new Cookie(name, null);
				if (!StringUtil.isEmpty(domain))
					cookie.setDomain(domain);
				cookie.setMaxAge(0);
				cookie.setPath("/");
				response.addCookie(cookie);
			}
		}
	}

	static public void clearCookies(HttpServletRequest request, HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return;
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = new Cookie(cookies[i].getName(), null);
			if (!StringUtil.isEmpty(domain))
				cookie.setDomain(domain);
			cookie.setMaxAge(0);
			cookie.setPath("/");
			response.addCookie(cookie);
		}

	}

	static public void printCookies(HttpServletRequest request) {

		Cookie[] cookies = request.getCookies();
		if (cookies == null)
			return;
		for (int i = 0; i < cookies.length; i++) {
			System.out.println("cookies:" + cookies[i].getName() + "=" + cookies[i].getValue());
		}
	}

}
